package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;

    @GetMapping("/page")
    public R<IPage<Category>> getCategoriesByPage(Integer page, Integer pageSize) {

        IPage<Category> categoryList=categoryService.getCategoriesByPage(page, pageSize);
        R<IPage<Category>> iPageR=new R<>();
        iPageR.setData(categoryList);
        iPageR.setCode(1);
        return iPageR;
    }

    @PostMapping
    public R<Category> addCategory(@RequestBody Category category, HttpServletRequest request) {
        Boolean flag=categoryService.addCategory(category);
        //设置 添加菜品的时间+创建人
       /* category.setCreateTime(LocalDateTime.now());
        category.setCreateUser(loginUser.getId());*/

        R<Category> iPageR=new R<>();
        iPageR.setCode(flag ? 1 : 0);
        iPageR.setMsg(flag ? category.getType() == 1 ? "添加套餐分类成功" : "添加套餐分类失败" : category.getType() == 2 ? "添加菜品分类成功" : "添加菜品分类失败");
        return iPageR;
    }

    @PutMapping
    public R<Category> updateCategory(@RequestBody Category category) {
        boolean flag=categoryService.updateById(category);
        R<Category> categoryR=new R<>();
        categoryR.setCode(flag ? 1 : 0);
        categoryR.setMsg(flag ? "修改分类成功" : "修改分类失败" );
        return categoryR;
    }

    @DeleteMapping
    public R<String> deleteCategory(Long ids) {
        boolean flag=categoryService.removeById(ids);
        //删除套餐前要先判断 该品类下是否有菜品
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,ids);
        int dishCount=dishService.count(wrapper);
        if (dishCount > 0){
            return R.error("该分类下有菜品绑定!不可删除");
        }
        //删除套餐前要先判断 该品类下是否有套餐
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,ids);
        int setmealCount=dishService.count(wrapper);
        if (setmealCount > 0){
            return R.error("该分类下有套餐绑定!不可删除");
        }

        return flag ? R.success("删除分类成功") : R.error("删除分类失败");
    }

    @GetMapping("/list")
    public R<List<Category>> getCategoryListByType(Integer type) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(type != null ,Category::getType,type);
        List<Category> list=categoryService.list(lambdaQueryWrapper);

        return R.success(list);
    }

}
